package com.lmuse.xmr.foodspotting.ui.classify;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.listeners.OnDismissCallback;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.RecipePresenterImpl;
import com.lmuse.xmr.foodspotting.model.Recipe;
import com.lmuse.xmr.foodspotting.ui.recipe.RecipeActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

public class ClassifyFragment extends Fragment implements Contract.RecipeView{

    private ExpandableListView classifyView;
    private MaterialListView cardList;
    private TextView labelTitle;

    private Contract.RecipePresenter recipePresenter;

    private int groupCount;
    private int page = 0;//当前页数
    private int limit = 500; //每页个数 || 查询条数
    private List<Recipe> recipeList;

    private String[][] childStrings = {
            {"早餐","午餐","晚餐"},
            {"家常菜","蒸菜","炖菜","炒菜","素食","瘦身餐","汤类","主食"},
            {"川菜","湘菜","东北菜","清真菜","日本料理","韩国料理"},
            {"甜点","西点"}
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, container, false);
        recipePresenter = new RecipePresenterImpl(this);
        initView(view);
        return view;
    }

    void initView(View view) {
        labelTitle = (TextView) view.findViewById(R.id.tv_label_title);
        classifyView = (ExpandableListView) view.findViewById(R.id.expand_classify);
        classifyView.setAdapter(new ExpandableAdapter());
        classifyView.setOnGroupClickListener(new GroupClickListener());
        classifyView.setOnChildClickListener(new ChildClickListener());
        cardList = (MaterialListView) view.findViewById(R.id.cardlist_classify);
        cardList.addOnItemTouchListener(new CardClickListener());
        cardList.setOnDismissCallback(new CardDismissCallBack());

        groupCount = classifyView.getCount();
        for (int i=0; i<groupCount; i++) {
            classifyView.expandGroup(i);
        }
        recipeList = new ArrayList<>();

        recipePresenter.queryRecipe("label", "早餐", limit);
    }

    class GroupClickListener implements ExpandableListView.OnGroupClickListener {
        @Override
        public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
            //返回false展开分组
            return false;
        }
    }

    class ChildClickListener implements ExpandableListView.OnChildClickListener {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            page = 0;
            labelTitle.setText(childStrings[i][i1]);
            recipePresenter.queryRecipe("label",childStrings[i][i1], limit);
            return false;
        }
    }

    class CardClickListener implements RecyclerItemClickListener.OnItemClickListener {

        @Override
        public void onItemClick(@NonNull Card card, int position) {
            Intent startIntent = new Intent(getActivity(),
                    RecipeActivity.class);
            startIntent.putExtra(com.lmuse.xmr.foodspotting.constants.Recipe.ID,
                    recipeList.get(position).getObjectId());
            startActivity(startIntent);
        }

        @Override
        public void onItemLongClick(@NonNull Card card, int position) {

        }
    }

    class CardDismissCallBack implements OnDismissCallback {
        @Override
        public void onDismiss(@NonNull Card card, int position) {
            /*int count = (page + 1) * limit - 2;
            if (position == count) {
                Toast.makeText(getActivity(), "当前第" + count + "个", Toast.LENGTH_SHORT).show();
                page++;
                recipePresenter.queryRecipe("label", labelTitle.getText().toString(), limit, page);
            }*/
        }
    }

    @Override
    public void onUploadingImg(Integer value) {

    }

    @Override
    public void onUploadingImg(int curIndex, int curPercent, int total, int totalPercent) {

    }

    @Override
    public void onUploadImgSuccess(BmobFile cover) {

    }

    @Override
    public void onUploadImgSuccess(List<String> urls) {

    }

    @Override
    public void onUploadImgFailed(String msg) {

    }

    @Override
    public void onAddRecipeSuccess() {

    }

    @Override
    public void onAddRecipeFailed(String msg) {

    }

    @Override
    public void onQueryRecipeSuccess(Recipe recipe) {

    }

    @Override
    public void onQueryRecipeSuccess(List<Recipe> recipeList) {
        this.recipeList = recipeList;
//        Toast.makeText(getActivity(), "请求完成", Toast.LENGTH_SHORT).show();
//        if (page == 0) { //切换标签的时候清除上一个加载的标签数据
            cardList.getAdapter().clearAll();
//        }
        for (Recipe recipe : recipeList) {
            Card card = new Card.Builder(getActivity())
                    .setTag("BIG_IMAGE_CARD")
                    .withProvider(new CardProvider())
                    .setLayout(R.layout.material_big_image_card_layout)
                    .setTitle(recipe.getTitle())
                    .setTitleColor(getResources().getColor(R.color.colorWhite))
                    .setDrawable(recipe.getCoverUrl())
                    .endConfig()
                    .build();
            cardList.getAdapter().add(card);
        }
        cardList.smoothScrollToPosition(0);
    }

    @Override
    public void onQueryRecipeFailed(String msg) {
        Toast.makeText(getActivity(), "请求数据失败!(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onQueryKeySuccess(List<Recipe> recipeKeyList) {

    }

    @Override
    public void onQueryKeyFailed(String msg) {

    }

    @Override
    public void onDeleteRecipeSuccess() {

    }

    @Override
    public void onDeleteRecipeFailed(String msg) {

    }
}
